<?php


class ChongzhiController extends CommonController
{
    const CHONGZHILIST = [
        'ctcc100' => ['product_id' => 83, 'goods_id' => 771, 'sku_price_id' => 41143],
        'ctcc200' => ['product_id' => 84, 'goods_id' => 772, 'sku_price_id' => 41144],
        'cmcc100' => ['product_id' => 79, 'goods_id' => 771, 'sku_price_id' => 41143],
        'cmcc200' => ['product_id' => 80, 'goods_id' => 772, 'sku_price_id' => 41144],
        'cucc100' => ['product_id' => 81, 'goods_id' => 771, 'sku_price_id' => 41143],
        'cucc200' => ['product_id' => 82, 'goods_id' => 772, 'sku_price_id' => 41144],
    ];
    const CHONGZHIURL = 'http://124.221.119.187/yrapi.php/index/recharge';
    const CHECKURL = 'http://124.221.119.187/yrapi.php/index/check';
    const NOTIFYURL = 'http://xyc.zmssp.cn/chongzhi/notify';
    const USERID = '10009';
    const APIKEY = 'jOUQnqvcMZesGP2NkB6iJpHla4y3CrXD';

    protected $authExcept = ['notify'];

    public function chongzhiAction()
    {
        $money = $this->get('money');
        $mobile = $this->get('mobile');
        $pd = $this->matchprovider($mobile);
        if (empty($pd)) {
            json(['code' => 0, 'msg' => '手机号格式不正确或未知的运营商']);
            exit;
        }
        $info = self::CHONGZHILIST[$pd . $money] ?? null;
        if (empty($info)) {
            json(['code' => 0, 'msg' => '充值金额有误']);
            exit;
        }
        $order = orderModel::createOrder([
            'buy_type' => 'alone',
            'from' => 'goods',
            'order_type' => 'goods',
            'address_id' => 0,
            'coupons_id' => 0,
            'groupon_id' => 0,
            'delivery_card_id' => 0,
            'goods_list' => [['goods_id' => $info['goods_id'], 'goods_num' => 1, 'sku_price_id' => $info['sku_price_id']]],
        ]);
        $ext = json_decode($order->ext, true);
        $ext['chongzhi_mobile'] = $mobile;
        $ext['chongzhi_product_id'] = $info['product_id'];
        $ext['chongzhi_amount'] = intval($money);
        $order->ext = json_encode($ext);
        $order->save();
        json(['code' => 1, 'msg' => '', 'data' => $order]);
    }

    public function notifyAction()
    {
        $data = $_POST;
        \Log::out('话费充值NOTIFY：' . json_encode($data), 'chongzhi');
        unset($data['sign']);
        ksort($data);
        $sign_str = urldecode(http_build_query($data)) . '&apikey=' . self::APIKEY;
        $mysign = strtoupper(md5($sign_str));
        if ($mysign == $_POST['sign']) {
            $order = orderModel::where('order_sn', $data['out_trade_num'])->first();
            if ($order) {
                $order_ext = json_decode($order->getRawOriginal('ext'), true);
                $order_ext['chongzhi_notified'] = 1;
                $order_ext['chongzhi_state'] = $data['state'];
                $order->ext = json_encode($order_ext);
                $order->save();
            }
            echo 'success';
        } else {
            echo 'fail';
        }
    }

    public static function sign($param)
    {
        ksort($param);
        $sign_str = http_build_query($param) . '&apikey=' . self::APIKEY;
        return strtoupper(md5(urldecode($sign_str)));
    }

    private function matchprovider($mobile)
    {
        if (!preg_match("/^1([38][0-9]|4[579]|5[0-3,5-9]|6[6]|7[0135678]|9[89])\d{8}$/", $mobile)) {
            return '';
        }
        $ChinaTelecomList = Array('133','149','153','173','177','180','181','189','199');
        $ChinaMobileList  = Array('134','135','136','137','138','139','147','150','151','152','157','158','159','172','178','182','183','184','187','188','198');
        $ChinaUnicomList  = Array('130','131','132','145','155','156','166','171','175','176','185','186','166');
        $head = substr($mobile, 0, 3);
        if (in_array($head, $ChinaTelecomList)) {
            return 'ctcc';
        }
        if (in_array($head, $ChinaMobileList)) {
            return 'cmcc';
        }
        if (in_array($head, $ChinaUnicomList)) {
            return 'cucc';
        }
        return '';
    }
}
